unit nomenclature;

{$mode objfpc}{$H+}

interface

uses
  Classes, ZAbstractRODataset, Dialogs, Controls, Forms, Graphics,
  DBGrids, SysUtils, StdCtrls, Buttons, Grids, ExtCtrls, ComCtrls, types;

type

  { TfrmNomenclature }

  TfrmNomenclature = class(TForm)
    btnDelete: TBitBtn;
    btnFilterUnSet: TBitBtn;
    btnFilterSet: TBitBtn;
    btnClose: TBitBtn;
    btnCreate: TBitBtn;
    btnAddGroup: TBitBtn;
    btnEdit: TBitBtn;
    btnMove: TBitBtn;
    DBGNomenclature: TDBGrid;
    gbFilter: TGroupBox;
    ledtTitle: TLabeledEdit;
    ledtArticleM: TLabeledEdit;
    ledtArticleInside: TLabeledEdit;
    procedure BitBtn3Click(Sender: TObject);
    procedure btnAddGroupClick(Sender: TObject);
    procedure btnCreateClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure btnMoveClick(Sender: TObject);
    procedure DBGNomenclatureDblClick(Sender: TObject);
    procedure DBGNomenclatureDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure FormShow(Sender: TObject);
    procedure GetGroupTitle;
  private
    { private declarations }
  public
    { public declarations }
    CurrentGroupRecordNumber:Integer;
    CurrentElementRecordNumber:Integer;
  end;

var
  frmNomenclature: TfrmNomenclature;
  LastRecNo:String;
  groupTitle:String;
  parentGroupTitle:String;
implementation

{$R *.lfm}

{ TfrmNomenclature }
uses main,nomenclaturecreateedit,groupselect,nomenclaturecreateeditgroup;

procedure TfrmNomenclature.GetGroupTitle;
var
  parentid:String;
begin
  groupTitle:='';
  parentGroupTitle:='';
  if (CurrentGroupRecordNumber<>0)  then begin
     FrmMain.ZQuery.Active:=False;
     FrmMain.ZQuery.SQL.Text:='SELECT title,parentid FROM nomenclature WHERE id='+IntToStr(CurrentGroupRecordNumber);
     FrmMain.ZQuery.Active:=True;
     groupTitle:=FrmMain.DSQuery.DataSet.FieldByName('title').AsString;
     parentid:=FrmMain.DSQuery.DataSet.FieldByName('parentid').AsString;
  end;
  if (parentid<>EmptyStr) AND (parentid<>'0') then begin
    FrmMain.ZQuery.Active:=False;
    FrmMain.ZQuery.SQL.Text:='SELECT title FROM nomenclature WHERE id='+parentid;
    FrmMain.ZQuery.Active:=True;
    parentGroupTitle:=FrmMain.DSQuery.DataSet.FieldByName('title').AsString;
  end else begin
    parentGroupTitle:='';
  end;
  frmNomenclatureCreateEditGroup.edtGroup.Text:=parentGroupTitle;
  frmNomenclatureCreateEdit.edtGroup.Text:=groupTitle;
  FrmMain.ZQuery.Active:=False;
end;

procedure TfrmNomenclature.btnCreateClick(Sender: TObject);
begin
  GetGroupTitle;
  FrmMain.DSNomenclature.DataSet.Append;
  FrmMain.DSNomenclature.DataSet.FieldByName('quantity').AsInteger:=0;
  frmNomenclatureCreateEdit.ShowModal;
end;

procedure TfrmNomenclature.btnDeleteClick(Sender: TObject);
var
  res:Integer;
begin
  res:=MessageDlg('','Переконайтесь що запис ніде не використовується, Видалити запис?',mtConfirmation,mbYesNo,0);
  if (res=mrYes) then begin
    FrmMain.ZQuery.SQL.Text:='DELETE FROM nomenclature WHERE id='+DBGNomenclature.DataSource.DataSet.FieldByName('id').AsString;
    FrmMain.ZQuery.ExecSQL;
    FrmMain.DSNomenclature.DataSet.Refresh;
  end;
end;

procedure TfrmNomenclature.btnEditClick(Sender: TObject);
begin
  if (DBGNomenclature.DataSource.DataSet.FieldByName('group').AsBoolean) then begin
    //CurrentGroupRecordNumber:=DBGNomenclature.DataSource.DataSet.FieldByName('id').AsInteger;
    frmNomenclatureCreateEditGroup.edtGroupTitle.Text:=DBGNomenclature.DataSource.DataSet.FieldByName('title').AsString;
    CurrentElementRecordNumber:=DBGNomenclature.DataSource.DataSet.FieldByName('id').AsInteger;
    GetGroupTitle;
    frmNomenclatureCreateEditGroup.isEdit:=True;
    frmNomenclatureCreateEditGroup.ShowModal;
  end else begin
    GetGroupTitle;
    frmNomenclatureCreateEdit.ShowModal;
  end;
end;

procedure TfrmNomenclature.btnMoveClick(Sender: TObject);
begin
  if (DBGNomenclature.DataSource.DataSet.FieldByName('group').AsBoolean) then begin
    CurrentGroupRecordNumber:=DBGNomenclature.DataSource.DataSet.FieldByName('id').AsInteger;
    frmGroupSelect.groupsId:=1;
    frmGroupSelect.ShowModal;
    if (frmGroupSelect.selectedGroupId<>EmptyStr) then begin
      FrmMain.DBQuery.SQL.Text:='UPDATE nomenclature SET parentid='+frmGroupSelect.selectedGroupId+' WHERE id='+IntToStr(CurrentGroupRecordNumber);
      FrmMain.DBQuery.ExecSQL;
      FrmMain.DSNomenclature.DataSet.Refresh;
    end;
  end else begin
    CurrentGroupRecordNumber:=DBGNomenclature.DataSource.DataSet.FieldByName('id').AsInteger;
    frmGroupSelect.groupsId:=1;
    frmGroupSelect.ShowModal;
    if (frmGroupSelect.selectedGroupId<>EmptyStr) then begin
      FrmMain.DBQuery.SQL.Text:='UPDATE nomenclature SET parentid='+frmGroupSelect.selectedGroupId+' WHERE id='+DBGNomenclature.DataSource.DataSet.FieldByName('id').AsString;
      FrmMain.DBQuery.ExecSQL;
      FrmMain.DSNomenclature.DataSet.Refresh;
    end;
  end;
end;

procedure TfrmNomenclature.btnAddGroupClick(Sender: TObject);
begin
  GetGroupTitle;
  frmNomenclatureCreateEditGroup.edtGroupTitle.Text:=EmptyStr;
  frmNomenclatureCreateEditGroup.isNew:=True;
  frmNomenclaturecreateeditgroup.ShowModal;
end;

procedure TfrmNomenclature.BitBtn3Click(Sender: TObject);
begin
  if (DBGNomenclature.DataSource.DataSet.FieldByName('group').AsBoolean=False) then begin
    FrmMain.nomenclatureSelectedId:=DBGNomenclature.DataSource.DataSet.FieldByName('id').AsInteger;
    Close;
  end;
end;

procedure TfrmNomenclature.DBGNomenclatureDblClick(Sender: TObject);
var
  id,parentid:String;
  group:Boolean;
begin
  id:=DBGNomenclature.DataSource.DataSet.FieldByName('id').AsString;
  parentid:=DBGNomenclature.DataSource.DataSet.FieldByName('parentid').AsString;
  group:=DBGNomenclature.DataSource.DataSet.FieldByName('group').AsBoolean;
  if (group)  AND (LastRecNo=id) then begin
    FrmMain.ZTNomenclature.Filtered:=False;
    FrmMain.ZTNomenclature.SortedFields:='id,group,title';
    FrmMain.ZTNomenclature.SortType:=stAscending;
    FrmMain.ZTNomenclature.Filter:='id='+parentid+' or parentid='+parentid;
    FrmMain.ZTNomenclature.Filtered:=True;
    LastRecNo:=parentid;
    CurrentGroupRecordNumber:=StrToInt(parentid);
  end else begin
  if ((group) AND (parentid<>id))
  then begin
    FrmMain.ZTNomenclature.Filtered:=false;
    FrmMain.ZTNomenclature.SortedFields:='id,group,title';
    FrmMain.ZTNomenclature.SortType:=stAscending;
    FrmMain.ZTNomenclature.Filter:='id='+id+' or parentid='+id;
    FrmMain.ZTNomenclature.Filtered:=true;
    LastRecNo:=id;
    CurrentGroupRecordNumber:=StrToInt(id);
  end;
  end;
  if (group=False) then begin
    GetGroupTitle;
    frmNomenclatureCreateEdit.ShowModal;
  end;
end;

procedure TfrmNomenclature.DBGNomenclatureDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
bmp: TBitmap;
begin
    if (Column.FieldName = 'id') and (DBGNomenclature.DataSource.DataSet.FieldByName('group').AsBoolean) then begin
       bmp := TBitmap.Create;
      if (Column.Field.AsString=LastRecNo) then begin
        FrmMain.ImageList2.GetBitmap(3,bmp);
      end else begin
        FrmMain.ImageList2.GetBitmap(0,bmp);
      end;
       DBGNomenclature.Canvas.Draw(Rect.Left,Rect.Top,bmp);
       bmp.Free;
    end else if (Column.FieldName = 'id') then begin
       bmp := TBitmap.Create;
       FrmMain.ImageList2.GetBitmap(1,bmp);
       DBGNomenclature.Canvas.Draw(Rect.Left,Rect.Top,bmp);
       bmp.Free;
    end;
end;

procedure TfrmNomenclature.FormShow(Sender: TObject);
begin
  FrmMain.ZTNomenclature.Refresh;
  FrmMain.ZTNomenclature.Filtered:=False;
  FrmMain.ZTNomenclature.SortedFields:='id,group,title';
  FrmMain.ZTNomenclature.SortType:=stAscending;
  FrmMain.ZTNomenclature.Filter:='parentid=0';
  FrmMain.ZTNomenclature.Filtered:=True;

  CurrentGroupRecordNumber:=0;
end;

end.

